package week.Aug0810;

import java.util.Arrays;

/**
 * @author liangyi
 * @Date 2022/8/10
 * 子矩阵翻转
 * 链接：https://leetcode.cn/problems/reverse-submatrix/
 */
public class ReverseSubmatrix {
    public static void main(String[] args) {
        int[][] grid = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        int x = 1, y = 0, k = 3;
        int[][] child = reverseSubmatrix(grid, x, y, k);
        System.out.println(Arrays.deepToString(child));
        System.out.println(Arrays.deepToString(grid));
    }
    public static int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
        // 遍历需要交换的行（前k/2行）
        for (int i = 0; i < k / 2; i++) {
            int topRow = x + i;
            int bottomRow = x + k - 1 - i;
            // 交换两行在子矩阵列范围内的元素
            for (int j = y; j < y + k; j++) {
                int temp = grid[topRow][j];
                grid[topRow][j] = grid[bottomRow][j];
                grid[bottomRow][j] = temp;
            }
        }
        return grid;
    }
}
